#include "Tree.h"

namespace PeanutQuest {

	Tree::Tree(cg::Vector3f pos) {
		position = pos;
	}
	Tree::~Tree() {
	}

	inline
	void Tree::makeModel() {
		_modelDL = glGenLists(1);
		assert(_modelDL != 0);
		glNewList(_modelDL,GL_COMPILE);
			//tronco
			glPushMatrix();
			glRotated(-90,1,0,0);
			glColor3f(0.36f,0.2f,0.1f);
			glutSolidCylinder(1,5,20,20);
			

			//cone
			glTranslated(0,0,6);
			glColor3f(0.0f,0.8f,0.0f);
			glutSolidSphere(3,20,20);
			glPopMatrix();
		glEndList();
	}

	inline
	void Tree::makeMaterial() {
		GLfloat mat_emission[] = {0.0f,0.0f,0.0f,1.0f};
		GLfloat mat_ambient[] = {0.0f,0.0f,0.0f,1.0f};
		GLfloat mat_diffuse[] = {.0f,0.8f,0.0f,1.0f};
		GLfloat mat_specular[] = {0.9f,0.9f,0.9f,1.0f};
		GLfloat mat_shininess[] = {10.0f};

		_materialDL = glGenLists(1);
		assert(_materialDL != 0);
			glNewList(_materialDL,GL_COMPILE);
				glMaterialfv(GL_FRONT,GL_EMISSION ,mat_emission);
				glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);
				glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);
				glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
				glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
			glEndList();
	}

	void Tree::init() {
		makeModel();
		makeMaterial();
	}

	void Tree::draw() {

		//tree
		glPushMatrix();

		glTranslatef(position[0],position[1],position[2]);
		
		glCallList(_materialDL);
		glCallList(_modelDL);
		
		glPopMatrix();
	}
}